﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using qna.Business;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Script.Services;

namespace qna.Services
{
    /// <summary>
    /// Summary description for AnswerService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
	[ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AnswerService : System.Web.Services.WebService
    {
        //[WebMethod]
        //public IList<Answer> GetAnswers()
        //{
        //    return  new Business.Answer().GetAllAnswers();
        //}

        [WebMethod]
        public Answer GetID(int aID)
        {
            return (Answer)(new Business.Answer()).GetById(aID);
        }

        [WebMethod]
        public Answer[] GetByQID(int qID)
        {
            return new Business.Answer().GetByQuestionID(qID).ToArray();
        }

        [WebMethod]
        public Answer InsertAns(string content, int qID, int uID, DateTime dt)
        {
            Answer a = new Answer();
            a.Content = content;
            a.QuestionID = qID;
            a.UserId = uID;
            a.DateTime = dt;
            return a.Insert() == 0 ? null : a;
        }

        [WebMethod]
        public Answer UpdateAns(int aID, string content, DateTime dt)
        {
            Answer a = GetID(aID);
            if (a == null)
            {
                return null;
            }
            else
            {
                a.Content = content;
                a.DateTime = dt;
                var attrs = a.Attributes;
                a.Update(attrs);
                return a;
            }
        }

        [WebMethod]
        public bool DeleteAns(int aID)
        {
            Answer a = GetID(aID);
            if (a != null)
            {
                a.Id = aID;
                return a.Delete();
            }
            return false;
        }

        [WebMethod]
        public string GetUserNameAns(int aID)
        {
            Answer a = GetID(aID);
            string uname = "";

            if (a != null)
            {
                uname = a.GetUserName(a.Id);
            }

            return uname;
        }

        [WebMethod]
        public bool SetRating(int aID, int uID)
        {
            Answer a = GetID(aID);
            bool returnVal = a.SetAnswerRating(aID, uID);

            return returnVal;
        }
        [WebMethod]
        public int GetRating(int aID)
        {
            Answer a = GetID(aID);
            int returnVal = a.GetAnswerRating(aID);

            return returnVal;
        }

        [WebMethod]
        public bool CheckIfUserRated(int aID, int uID)
        {
            Answer a = GetID(aID);
            bool returnVal = a.CheckIfUserRated(aID, uID);

            return returnVal;
        }
    
    }
}
